package kayao.server.network.message;

import kayao.common.KayaoValues;
import kayao.common.NetworkUtils;
import kayao.server.AreaManager;
import kayao.server.DesktopServer;
import kayao.server.GameManager;

public class MessageThread extends Thread {

	protected boolean mRunning=true;
	protected boolean mDead=false;
	
	public MessageThread()
	{
		
	}

	public boolean terminate(){
		mRunning = false;
		while(!mDead){
			synchronized(this){
				this.notify();
			}
			System.out.println("[MessageThread] - PROBLEM: STUCK!");
			Thread.yield();
		}
		return false;
	}

	
	public void run()
	{
		System.out.println("[MessageThread] It's started");
		while(mRunning)
		{
			synchronized(this) {
				try 
				{
					this.wait();
					} 
				catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			//The notify method is called
			treatMessages();
		}
		mDead = true;
	}
	
	private void treatMessages() {
		IncomingMessage m;
		while((m=MessageHandler.getMessageIncoming())!= null)
		{
			if (DesktopServer.find(m.getIp()) == null){
				if(m.getData()[0]==KayaoValues.PLAYING_REQUEST_MESSAGE)
				{
					//Create thread to treat it
					System.out.println(DesktopServer.getTimestamp()+" [Server] Login request from: " + m.getIp());
					MessageHandler.insertOutgoingMessage(new LoginMessage(m));
				}
			}
			//First byte is the type of the message or the magic number
			else if(NetworkUtils.insertByteArrayToInt(m.getData(), 0)!=KayaoValues.MAGIC_NUMBER)
			{
				System.out.println(DesktopServer.getTimestamp()+" [Server] Bad request from: " + m.getIp() + ". It failed the magicnumber");
			}
			else if(m.getData()[4]==KayaoValues.WHAT_TIME_IS_IT_MESSAGE){
				System.out.println(DesktopServer.getTimestamp()+" [Server] Time request from: " + m.getIp());					
				MessageHandler.insertOutgoingMessage(new TimeMessage(m.getIp())); //port??
			}
			else if(m.getData()[4]==KayaoValues.LOGOUT_MSG){
				System.out.println(DesktopServer.getTimestamp()+" [Server] LogOut request from: " + m.getIp());					
				DesktopServer.removeClient(DesktopServer.find(m.getIp()));
			}
			else if(m.getData()[4]==KayaoValues.KING_REQUEST_MESSAGE)
			{
				//Create thread to treat it
				System.out.println(DesktopServer.getTimestamp()+" [Server] Kings request from: " + m.getIp());
				MessageHandler.insertOutgoingMessage(new KingsRequestMessage(m));
			}
			else if(m.getData()[4]==KayaoValues.DEAD_CLIENT)
			{
				//When we get this, we disable the client bing a king
				System.out.println(DesktopServer.getTimestamp()+" [Server] DeadClient notify from: " + m.getIp());
				System.out.println("[Server] Dead client ID: " + NetworkUtils.insertByteArrayToInt(m.getData(), 5)+"\n");
				AreaManager.deleteKingById(NetworkUtils.insertByteArrayToInt(m.getData(), 5));
			}
			else if(m.getData()[4]==KayaoValues.FLAG_COLLISION)
			{
				//[ MAGIC NUMBER (int32) | FLAG_COLISION (b) | Mission ID (int32) | TIMESTAMP ]
				System.out.println("[Server] Getting colision from: " + m.getIp());
				//treat the collision
				int missionId=NetworkUtils.insertByteArrayToInt(m.getData(), 5);
				int idApplicant=DesktopServer.find(m.getIp()).getId();
				int time=NetworkUtils.insertByteArrayToInt(m.getData(),9);
				GameManager.addCollision(idApplicant, missionId,time);
			}	
			else System.out.println("[MessageThread] - Message not treated in MessageThread.treatMessages() msgid: "+m.getData()[4]);
		}
	}
		
	
}
